package com.face.dao;

import com.face.common.Page;
import com.face.entity.Account;
import com.face.exception.DatabaseException;
import com.face.mapper.AccountMapper;
import com.face.wrapper.AccountWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AccountDao {

    @Autowired
    private AccountMapper accountMapper;

    public AccountWrapper findByUsername(String username) {
        return accountMapper.findByUsername(username);
    }

    public void save(Account account) {
        accountMapper.save(account);
    }

    public void active(String activeCode) {
        accountMapper.active(activeCode);
    }

    public Account findByEmail(String email) {
        return accountMapper.findByEmail(email);
    }

    public Page<AccountWrapper> listAll(String shopId, Integer page, Integer limit, String order) {
        PageHelper.startPage(page,limit);
        List<AccountWrapper> list = accountMapper.listAll(shopId, order);
        PageInfo<AccountWrapper> pageInfo = new PageInfo(list);
        return new Page<>(pageInfo.getTotal(),pageInfo.getList());
    }

    public Page<AccountWrapper> list(String shopId, Integer page, Integer limit, String username, String realname, String order) {
        PageHelper.startPage(page,limit);
        List<AccountWrapper> list = accountMapper.list(shopId, username, realname, order);
        PageInfo<AccountWrapper> pageInfo = new PageInfo(list);
        return new Page<>(pageInfo.getTotal(),pageInfo.getList());
    }

    public Page<AccountWrapper> listByRoleId(String shopId, Integer page, Integer limit, String username, String realname, Integer roleId, String order) {
        PageHelper.startPage(page,limit);
        List<AccountWrapper> list = accountMapper.listByRoleId(shopId, username, realname, roleId, order);
        PageInfo<AccountWrapper> pageInfo = new PageInfo(list);
        return new Page<>(pageInfo.getTotal(),pageInfo.getList());
    }

    public String saveAccount(Account account) {
        Integer i =accountMapper.saveAccount(account);
        if(i>0)
            return account.getUsername();;
        throw new DatabaseException("tb_account", "save", "account");
    }

    public Integer updateAccount(Account account){
        return accountMapper.updateAccount(account);
    }

    public Integer resetPassword(String username){
        return accountMapper.resetPassword(username);
    }

    public Integer deleteAccount(String username){
        return accountMapper.deleteAccount(username);
    }

    public List<String> findEnableNotifyOpenids(String shopId) {
        return accountMapper.findEnableNotifyOpenids(shopId);
    }

    public void changePassword(String username, String newPassword) {
        accountMapper.changePassword(username, newPassword);
    }

    public Account findByActiveCode(String activeCode) {
        return accountMapper.findByActiveCode(activeCode);
    }

    public void updateStatus(String shopId, int status) {
        accountMapper.updateStatus(shopId, status);
    }

    public void updateStatusByUsername(String username, int status) {
        accountMapper.updateStatusByUsername(username, status);
    }

    public Integer updateAccountWeixin(String username, String openId ) {
        return accountMapper.updateAccountWeixin(username,openId );
    }

    public String findUsernameByOpenid(String openid){
        return accountMapper.findUsernameByOpenid(openid);
    }

    public List<Account> findAllByShopId(String shopId) {
        return accountMapper.findAllByShopId(shopId);
    }

    public List<Account> findByRoleId(Integer roleId) {
        return accountMapper.findByRoleId(roleId);
    }

    public Account findManagerByShopId(String shopId) {
        return accountMapper.findManagerByShopId(shopId);
    }
}
